package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.DynamicConfigLab;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.utils.FileUtil;
import com.isunland.managesystem.utils.LogUtil;
import java.util.Date;

public abstract class EmpoyeeLoanFragment extends Fragment {
  protected static final int REQUEST_ADDEXTRA = 1;//附件
  protected static final int REQUEST_CHONG_FU = 2;//重命名。
  protected static final int SAVE = 3;//添加保存
  protected static final int SAVE_START = 4;//添加 保存并启动流程
  protected BaseVolleyActivity mActivity;
  protected CurrentUser mCurrentUser;
  protected TextView tvloanPerson;
  protected TextView tvloanProposer;
  protected TextView tvloanDate;
  protected TextView tvcertificateNo;//编码
  protected EditText etpoutAmount;//本次
  protected TextView tvsumBorrow;//合计
  protected EditText etexpenseDesc;
  //开户行
  protected EditText etpayeeBank;
  protected EditText etpayeeAccount;
  protected EditText etpayeeAccname;
  //附件
  protected TextView mTvExtra;//附件
  protected ImageView mIvExtra;//附件
  protected String selcurFile = "";//文件上传服务器返回路径
  protected String mFilePath = "";//文件本地路径
  protected String id = "";
  protected String mfileName = "";

  private Date date;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    mActivity = (BaseVolleyActivity) getActivity();
    mCurrentUser = CurrentUser.newInstance(getActivity());

    setTitle();
    super.onCreate(savedInstanceState);
  }

  /**
   * 设置标题
   */
  protected abstract void setTitle();

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_add_employeeloan, container, false);
    tvcertificateNo = (TextView) v.findViewById(R.id.tv_certificateNo);
    etexpenseDesc = (EditText) v.findViewById(R.id.et_expenseDesc);
    etpoutAmount = (EditText) v.findViewById(R.id.et_poutAmount);
    tvsumBorrow = (TextView) v.findViewById(R.id.tv_sumBorrow);
    tvloanPerson = (TextView) v.findViewById(R.id.tv_loanPerson);
    tvloanPerson.setText(mCurrentUser.getRealName());
    tvloanProposer = (TextView) v.findViewById(R.id.tv_loanProposer);
    tvloanProposer.setText(mCurrentUser.getRealName());
    tvloanDate = (TextView) v.findViewById(R.id.tv_loanDate);

    //开户行
    etpayeeBank = (EditText) v.findViewById(R.id.et_payeeBank);
    etpayeeAccname = (EditText) v.findViewById(R.id.et_payeeAccname);
    etpayeeAccount = (EditText) v.findViewById(R.id.et_payeeAccount);
    //附件
    mTvExtra = (TextView) v.findViewById(R.id.tv_extra);
    mIvExtra = (ImageView) v.findViewById(R.id.iv_extra);
    mIvExtra.setOnClickListener(new OnClickListener() {
      @Override public void onClick(View v) {
        DialogFragment dialog = ExtraUpLoadDialogFragment.newInstance(mFilePath);

        FragmentManager fm = getActivity().getSupportFragmentManager();
        dialog.setTargetFragment(EmpoyeeLoanFragment.this, REQUEST_ADDEXTRA);
        dialog.show(fm, "");
      }
    });
    //动态配置列   ---隐藏和显示，title
    DynamicConfigLab.newInstance(mActivity, getString(R.string.module_appFinance))
        .autoConfigTitle(v);
    DynamicConfigLab.newInstance(mActivity, getString(R.string.module_appFinance))
        .autoConfigVisiblity(v);
    initData();

    return v;
  }

  /**
   * 初始化控件数据。
   */
  protected void initData() {
    // Auto-generated method stub

  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;

    //附件
    if (requestCode == REQUEST_ADDEXTRA) {
      String result = data.getStringExtra(ExtraUpLoadDialogFragment.EXTRA_RESULT);
      LogUtil.i("result" + result);
      if (ExtraUpLoadDialogFragment.VALUE_CLEAR.equalsIgnoreCase(result)) {
        clearExtraFile();
      } else {

        mFilePath = result;
        LogUtil.i("mDocPath===" + mFilePath);
        if (mFilePath != null) {

          mfileName = FileUtil.getFileNameByFilePath(mFilePath);
          //					String[] names =fileName.split("\\.");
          //					mfileName =names[names.length-2]+"."+names[names.length-1];
          mTvExtra.setText(mfileName);
        }
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  public void clearExtraFile() {
    mFilePath = "";
    mfileName = "";
    mTvExtra.setText("");
  }
}
